﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Blogs
{
    public partial class AuditBlogOtherEdit : System.Web.UI.Page
    {
        private string id = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"] + "";
            if (!Page.IsPostBack)
            {
                LoadContent();
                BindList();
            }
        }

        #region 加载详情
        private void LoadContent()
        {
            Blog binfo = new Blog();
            BlogHelper bhelper = new BlogHelper();
            binfo = bhelper.GetBlog(id);
            lblBlogTitle.Text = "现创作：<a href=\"../b/"+id+".aspx\" target=\"_blank\">"+binfo.Title+"</a>";
        }
        #endregion

        #region 加载列表
        private void BindList()
        {
            BlogOtherEditHelper boehelper = new BlogOtherEditHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "BlogID", id);
            c.Add(CriteriaType.Equals, "Status", 0);
            List<BlogOtherEdit> boelist = boehelper.QueryBlogOtherEditBySearch(c, null);
            if (boelist != null && boelist.Count > 0)
            {
                rptBlogEditList.DataSource = boelist;
                rptBlogEditList.DataBind();
            }
            else
            {
                lblMessage.Text = "<font color=\"red\">已成功采纳其他人的编辑</font>";
            }
        }
        #endregion

        #region 列表事件
        protected void rptBlogEditList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            string getid = hidID.Value;

            if (e.CommandName == "detail")//详情
            {
                Response.Redirect("AuditBlogOtherEditDetail.aspx?id="+getid+"&blogid="+id);
            }
            else if (e.CommandName == "yes")//采纳
            {
                BlogOtherEdit boeinfo = new BlogOtherEdit();
                BlogOtherEditHelper boehelper = new BlogOtherEditHelper();
                boeinfo = boehelper.GetBlogOtherEdit(getid);
                string title = boeinfo.Title;
                string keyword = boeinfo.KeyWord;
                string blogcontent = boeinfo.BlogContent;

                Blog binfo = new Blog();
                BlogHelper bhelper = new BlogHelper();
                binfo = bhelper.GetBlog(id);
                binfo.Title = title;
                binfo.KeyWord = keyword;
                binfo.BlogContent = blogcontent;
                string firstpicpath = bhelper.GetFirstPicPath(blogcontent);
                if (firstpicpath != "")
                {
                    binfo.HasPic = 1;
                    if (firstpicpath.Contains("http"))
                    {
                        binfo.FirstPicPath = firstpicpath;
                    }
                    else
                    {
                        string serverpath = UserBase.GetServerPath();
                        binfo.FirstPicPath = serverpath + firstpicpath;
                    }
                }

                bhelper.EditBlog(binfo);
                //更新博客编辑表中状态
                boehelper.BlogEditAccept(getid, id);

                //给修改人发送消息
                Message minfo = new Message();
                MessageHelper mhelper = new MessageHelper();
                Users uinfo = UserBase.GetCurrentUser();
                minfo.FromUsersID = uinfo.ID;
                minfo.FromNickName = uinfo.NickName;
                minfo.ToUsersID = boeinfo.UsersID;
                minfo.MessageContent = "您对创作<a href=\"../b/"+id+".aspx\" target=\"_blank\">"+binfo.Title+"</a>的编辑得到了采纳，感谢！";
                minfo.Status = 0;
                minfo.Flag = 0;
                mhelper.AddMessage(minfo);

                Tools.AlertAndRedirect("采纳其他人编辑成功", "AuditBlogOtherEdit.aspx?id="+id);
            }
        }
        #endregion
    }
}